<template>
  <el-card class="good-container">
    <template #header>
      <div class="header">
        <el-button type="primary" :icon="Plus" @click="handleAdd">新增文章</el-button>
      </div>
    </template>
    <el-table
      :load="state.loading"
      :data="state.tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column
        prop="moduleCode"
        label="模块编号"
      >
      </el-table-column>
      <el-table-column
        prop="title"
        label="文章标题"
      >
      </el-table-column>
      <el-table-column
        prop="subTitle"
        label="文章描述"
      >
      </el-table-column>
      <el-table-column
        label="封面图片"
      >
        <template #default="scope">
          <img style="width: 100px; height: 100px;" :key="scope.row.id" :src="scope.row.coverPicture" alt="封面图">
        </template>
      </el-table-column>

      <el-table-column
        label="难度等级"
      >
        <template #default="scope">
          <span style="color: green;" v-if="scope.row.level == 0">简单</span>
          <span style="color: red;" v-else>较难</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="100"
      >
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>
          <a style="cursor: pointer; margin-right: 10px" v-if="scope.row.enable == 0" @click="handleStatus(scope.row.id, 1)">下线</a>
          <a style="cursor: pointer; margin-right: 10px" v-else @click="handleStatus(scope.row.id, 0)">上线</a>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="state.total"
      :page-size="state.pageSize"
      :current-page="state.pageIndex"
      @current-change="changePage"
    />
  </el-card>
</template>

<script setup>
import { onMounted, reactive, getCurrentInstance } from 'vue'
import axios from '@/utils/axios'
import { ElMessage } from 'element-plus'
import { Plus, Delete } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const app = getCurrentInstance()
const { goTop } = app.appContext.config.globalProperties
const router = useRouter()
const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  total: 0, // 总条数
  pageIndex: 1, // 当前页
  pageSize: 10, // 分页大小
})
onMounted(() => {
  getQuestionList()
})
// 获取轮播图列表
const getQuestionList = () => {
  state.loading = true
  axios.post('api/admin/question/v1/list', {
    pageIndex: state.pageIndex,
    pageSize: state.pageSize

  }).then(res => {
    console.log(res.records)
    state.tableData = res.records
    state.total = res.total
    state.pageIndex = res.current
    state.loading = false
    goTop && goTop()
  })
}
const handleAdd = () => {
  router.push({ path: '/add' })
}
const handleEdit = (id) => {
  router.push({ path: '/edit', query: { id } })
}
const changePage = (val) => {
  state.pageIndex = val
  getQuestionList()
}
const handleStatus = (id, status) => {
  axios.post(`api/admin/question/v1/update`, {
    id: id,
    enable: status
  }).then(() => {
    ElMessage.success('修改成功')
    getQuestionList()
  })
}
</script>

<style scoped>
  .good-container {
    min-height: 100%;
  }
  .el-card.is-always-shadow {
    min-height: 100%!important;
  }
</style>
